﻿@{
    ViewBag.Title = "UserList";
    Layout = "~/Views/Shared/_Main.cshtml";
}
@section Head{
    <style>
        .oracle,.mysql
        {
            display: none;
        }
        .file
        {
            display: none;
        }
        .txtright
        {
            text-align: right; 
            width: 100px;
        }
        .searchArea input
        {
            width: 200px;
        }
    </style>
    <script type="text/javascript">
        var UrlArray = $.getUrlArray();
        $(function () {
            var id = UrlArray["id"];
            if (id) {
                var parent = window.dialogArguments; //获取父页面参数
                jv.SetArrarValue($(".searchArea"), 4, parent.retDBData[id]);
                var hostArr = parent.retDBData[id].Host.split("|");
                var partArr = parent.retDBData[id].Part.split("|");
                $(".host").val(hostArr[0]);
                $(".part").val(partArr[0]);
                for (var i = 1; i < hostArr.length; i++) {
                    addOraHost(hostArr[i], partArr[i]);
                }
            }
            var dbType = UrlArray["dbType"];
            switch (dbType) {
                case "2":
                    $(".oracle").show();
                    break;
                case "4":
                case "8":
                    $(".mysql").show();
                    break;
                case "3":
                case "5":
                case "6":
                case "7":
                    $(".sql").hide();
                    $(".file").show();
                    break;
            }
        });
        function checksql() {
            if ($.trim($("#txt_Host").val()) == "") {
                alert("地址不能为空！");
                return false;
            }
            if ($.trim($("#txt_Server").val()) == "") {
                alert("数据库名不能为空！");
                return false;
            }
            if ($.trim($("#txt_User").val()) == "") {
                alert("用户名不能为空！");
                return false;
            }
            if ($.trim($("#txt_Pass").val()) == "") {
                alert("密码不能为空！");
                return false;
            }
            return true;
        }
        function checkOra() {
            if ($.trim($("#txt_Part").val()) == "") {
                alert("端口不能为空！");
                return false;
            }
            if ($.trim($("#txt_SID").val()) == "") {
                alert("SID不能为空！");
                return false;
            }
            return true;
        }
        function checkfile() {
            if ($.trim($("#txt_FileUrl").val()) == "") {
                alert("地址不能为空！");
                return false;
            }
            if ($.trim($("#txt_FileUser").val()) == "") {
                alert("用户名不能为空！");
                return false;
            }
            if ($.trim($("#txt_FilePass").val()) == "") {
                alert("密码不能为空！");
                return false;
            }
            return true;
        }
        function tryConn(save) {
            var dbType = UrlArray["dbType"];
            var conn = false;
            switch (dbType) {
                case "1":
                case "4":
                case "8":
                    conn = checksql();
                    break;
                case "2":
                    if (checksql()) {
                        conn = checkOra();
                    }
                    break;
                default:
                    conn = checkfile();
                    break;
            }
            var add = true;
            var parent = window.dialogArguments; //获取父页面
            var rtvalue = jv.GetDivArray($(".searchArea"), 4);
            if (dbType == "2") {
                rtvalue.Host = getArrValue(".host", "|");
                rtvalue.Part = getArrValue(".part", "|");
            }
            var v = 0;
            var id = UrlArray["id"];
            if (!id)
                id = -1;
            if (save) {
                for (var j = 0; j < parent.retDBData.length; j++) {
                    rtvalue.GUID = parent.retDBData[j].GUID;
                    if ($.toJSON(parent.retDBData[j]) == $.toJSON(rtvalue) && j != id) {
                        v++;
                        conn = false;
                    }
                }
                if (v > 0) {
                    alert("添加失败，存在相同的连接！");
                }
            }
            if (conn) {
                rtvalue.GUID = id;
                $(".loading").show();
                $.post("TestConeciton", { dbType: dbType, connectionString: $.toJSON(rtvalue) }, function (result) {
                    if (result == "1") {
                        if (save) {
                            returnParent(rtvalue);
                        }
                        else
                            alert("连接成功!");
                    } else {
                        alert(result);
                    }

                    $(".loading").hide();
                });
            }
        }
        var pNum = 1;
        function addOraHost(h, p) {
            $(".addOraHost").append("<div class='ph" + pNum + "'><input type='text' class='host' value='" + h + "' /> <span class='red'>*</span>端口：<input type='text' class='part' style='width: 90px;' value='" + p + "' /> <input type='button' style='width: 90px;' value='删除' onclick='delOraHost(" + pNum + ");' />");
            pNum++;
        } function delOraHost(v) {
            $(".ph" + v).remove();
        }
    </script>
}
 <div style="margin: 5px;">
    <table class="listTitle">
        <tr>
            <td width="15">
                <img src="/images/edit.png" />
            </td>
            <td width="220">
                添加数据库连接
            </td>
            <td align="right">
                <input type="button" value="保存" onclick="tryConn(true);" class="button" />
                <input type="button" value="测试连接" onclick="tryConn(false);" class="button" />
            </td>
        </tr>
    </table>
    <table class="searchArea" width="100%">
        <tr>
            <td valign="top">
                <div style="overflow-y: scroll; height: 160px;">
                    <table class="sql">
                        <tr>
                            <td class="txtright" valign="top">
                                <span class="red">*</span>地址：
                            </td>
                            <td>
                                <input type="hidden" id="txt_GUID" />
                                <input type="text" id="txt_Host" class="host" />
                                <span class="oracle"><span class="red">*</span>端口：<input type="text" id="txt_Part"
                                    style="width: 90px;" value="1521" class="part" />
                                    <input type="button" style="width: 90px;" value="添加" onclick="addOraHost('','1521');"
                                        class="button" />
                                </span>
                                <div class="addOraHost">
                                </div>
                            </td>
                        </tr>
                        <tr>
                            <td class="txtright">
                                <span class="red">*</span>数据库名：
                            </td>
                            <td>
                                <input type="text" id="txt_Server" />
                            </td>
                        </tr>
                        <tr>
                            <td class="txtright">
                                <span class="red">*</span>用户名：
                            </td>
                            <td>
                                <input type="text" id="txt_User" />
                            </td>
                        </tr>
                        <tr>
                            <td class="txtright">
                                <span class="red">*</span>密码：
                            </td>
                            <td>
                                <input type="password" id="txt_Pass" />
                            </td>
                        </tr>
                        <tr class="mysql">
                            <td class="txtright">
                                端口：
                            </td>
                            <td>
                                <input type="text" id="txt_Part" value="" />
                            </td>
                        </tr>
                        <tr class="oracle">
                            <td class="txtright">
                                SID：
                            </td>
                            <td>
                                <input type="text" id="txt_SID" value="SID" />
                            </td>
                        </tr>
                    </table>
                    <table class="file">
                        <tr>
                            <td class="txtright">
                                <span class="red">*</span>地址：
                            </td>
                            <td>
                                <input type="text" id="txt_FileUrl" style="width: 380px;" />
                            </td>
                        </tr>
                        <tr>
                            <td class="txtright">
                                <span class="red">*</span>用户名：
                            </td>
                            <td>
                                <input type="text" id="txt_FileUser" />
                            </td>
                        </tr>
                        <tr>
                            <td class="txtright">
                                <span class="red">*</span>密码：
                            </td>
                            <td>
                                <input type="password" id="txt_FilePass" />
                            </td>
                        </tr>
                    </table>
                    <table>
                        <tr>
                            <td class="txtright">
                                描述：
                            </td>
                            <td>
                                <input type="text" id="txt_Description" maxlength="240" style="width: 380px;" />
                                <input type="hidden" id="txt_Status" />
                            </td>
                        </tr>
                    </table>
                </div>
            </td>
        </tr>
    </table>
    <span class="loading">
        <img src="/Images/tree_loading.gif" />
    </span>
</div>
